package com.whu.exp1jsp.controller;

import com.whu.exp1jsp.dao.CartDAO;
import com.whu.exp1jsp.dao.ProductDAO;
import com.whu.exp1jsp.dao.PurchaseDAO;
import com.whu.exp1jsp.pojo.ProductSt;
import com.whu.exp1jsp.pojo.UserSt;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.cert.TrustAnchor;
import java.sql.SQLException;

@WebServlet(name = "purchaseServlet", value = "/purchase-servlet")
public class PurchaseServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        HttpSession session = request.getSession();
        String origin = request.getParameter("origin");
        if(origin == null){
            origin = "cart";
        }
        Long productId = Long.parseLong(request.getParameter("productId"));
        if(productId == null){
            request.setAttribute("errorMessage", "商品不存在");
            request.getRequestDispatcher("index.jsp").forward(request, response);
            return;
        }
        ProductDAO productDAO = new ProductDAO();
        ProductSt product = null;
        try {
            product = productDAO.selectProductById(productId);
        }catch (SQLException e){
            e.printStackTrace();
            request.setAttribute("errorMessage", "数据库错误");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
        if (product == null){
            request.setAttribute("errorMessage", "商品不存在");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
        product.setStatus(3);
        try{
            productDAO.updateProduct(product);
        }catch (SQLException e){
            request.setAttribute("errorMessage", "数据库错误");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }

        CartDAO cartDAO = new CartDAO();

        UserSt user = session.getAttribute("user") == null ? null : (UserSt) session.getAttribute("user");
        if (user == null){
            request.setAttribute("error", "请先登录");
            request.getRequestDispatcher("login.jsp").forward(request, response);
            return;
        }
        PurchaseDAO purchaseDAO = new PurchaseDAO();
        try {
            cartDAO.purchaseProduct(user.getUserId(), productId);
            purchaseDAO.addNewPurchase(product.getProductId(),product.getOwnerId(),user.getUserId());
        }catch (SQLException e){
            request.setAttribute("errorMessage", "数据库错误");
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
        request.setAttribute("successMessage", "购买成功");
        request.setAttribute("operation", "getProducts");
        String destination;
        if(origin.equals("cart")){
            destination = "cart-servlet";
        }else{
            destination = "products-servlet";
        }

        request.getRequestDispatcher(destination).forward(request, response);

    }
}
